<?php
/*********************/
/*                   */
/*  后台订单控制       */
/*   BY voiet        */
/*   2009-8-14更新    */
/*                   */
/*********************/

//require_once( CORE_DIR."/admin/controller/order/ctl.order.php" );
class cct_order extends ctl_order
{
	function printingmerge( $order_id )
	{
		if ( $_POST['order_id'] )
		{
			$aInput = $_POST['order_id'];
		}
		else if ( $order_id )
		{
			$aInput = array(
				$order_id
			);
		}
		else
		{
			$this->begin( "index.php?ctl=order/order&act=index" );
			$this->end( false, __( "打印失败：订单参数传递出错" ) );
			exit( );
		}
		foreach ( $aInput as $order_id )
		{
			$type = 4;
			$order = $this->system->loadmodel( "trading/order" );
			$member = $this->system->loadmodel( "member/member" );
			$dbTmpl = $this->system->loadmodel( "content/systmpl" );
			$order->setprintstatus( $order_id, $type, true );
			$orderInfo = $order->getfieldbyid( $order_id );
			$orderInfo['self'] = 0 - $orderInfo['discount'] - $orderInfo['pmt_amount'];
			$goodsItem = $order->getitemlist( $order_id );
			$memberInfo = $member->getfieldbyid( $orderInfo['member_id'], array( "point" ) );
			foreach ( $goodsItem as $k => $rows )
			{
				$aData = array( );
				$goodsItem[$k]['addon'] = unserialize( $rows['addon'] );
				if ( $rows['minfo'] && unserialize( $rows['minfo'] ) )
				{
					$goodsItem[$k]['minfo'] = unserialize( $rows['minfo'] );
				}
				else
				{
					$goodsItem[$k]['minfo'] = array( );
				}
			}
			$giftsItem = $order->getgiftitemlist( $order_id );
			$orderSum = $order->sumorder( $orderInfo['member_id'] );
			$orderSum['sum'] = $orderSum['sum'] ? $orderSum['sum'] : 0;
			$aData['goodsItem'] = $goodsItem;
			$aData['giftsItem'] = $giftsItem;
			$aData['orderInfo'] = $orderInfo;
			$aData['orderSum'] = $orderSum;
			$aData['memberPoint'] = $memberInfo['point'] ? $memberInfo['point'] : 0;
			$shopinfo = array(
				"name" => $this->system->getconf( "system.shopname" ),
				"url" => $this->system->getconf( "store.shop_url" ),
				"email" => $this->system->getconf( "store.email" ),
				"tel" => $this->system->getconf( "store.telephone" ),
				"logo" => $this->system->getconf( "site.logo" )
			);
			$aData['shop'] = $shopinfo;

			$aData['cart'] = $dbTmpl->fetch( "../admin/view/order/print_cart", array(
				"goodsItem" => $goodsItem,
				"giftsItem" => $giftsItem,
				"orderInfo" => $orderInfo,
				"shop" => $shopinfo,
				"orderSum" => $orderSum,
				"memberPoint" => $memberInfo['point'] ? $memberInfo['point'] : 0
			) );
			$aData['sheet'] = $dbTmpl->fetch( "../admin/view/order/print_sheet", array(
				"goodsItem" => $goodsItem,
				"giftsItem" => $giftsItem,
				"orderInfo" => $orderInfo,
				"orderSum" => $orderSum,
				"memberPoint" => $memberInfo['point'] ? $memberInfo['point'] : 0
			) );
			$this->pagedata['pages'][] = $dbTmpl->fetch( "../admin/view/e7/order/print", array(
				"order" => $aData
			) );
		}
		$this->setview( "e7/order/printmerge.html" );
		$this->output( );
		return;

	}
	
	function changepaystate( $orderid )
	{
			$objOrder = $this->system->loadmodel( "trading/order" );
			if ( $orderid )
			{
					$objOrder->op_id = $this->op->opid;
					$objOrder->op_name = $this->op->loginName;
					$order = $objOrder->tochangepay( $orderid );
					$this->detail( $orderid );
					exit( );
			}
	}
	
	
	function resumepay( $orderid )
	{
			$objOrder = $this->system->loadmodel( "trading/order" );
			if ( $orderid )
			{
					$objOrder->op_id = $this->op->opid;
					$objOrder->op_name = $this->op->loginName;
					$order = $objOrder->toresumepay( $orderid );
					$this->detail( $orderid );
					exit( );
			}
	}
	
	function changeshipstate( $orderid )
	{
			$objOrder = $this->system->loadmodel( "trading/order" );
			if ( $orderid )
			{
					$objOrder->op_id = $this->op->opid;
					$objOrder->op_name = $this->op->loginName;
					$order = $objOrder->tochangeship( $orderid );
					$this->detail( $orderid );
					exit( );
			}
	}
	
	
	function resumeshipstate( $orderid )
	{
			$objOrder = $this->system->loadmodel( "trading/order" );
			if ( $orderid )
			{
					$objOrder->op_id = $this->op->opid;
					$objOrder->op_name = $this->op->loginName;
					$order = $objOrder->toresumeship( $orderid );
					$this->detail( $orderid );
					exit( );
			}
	}
	
	function together( )
	{
			$this->path[] = array( "text" => "订单合并" );
			$this->page( "e7/order_together.html" );
	}
	
	
	
	function savetogether( )
	{
			$this->begin( "index.php?ctl=order/order&act=together" );
			$_POST['mainorder']=trim($_POST['mainorder']);
			$_POST['suborder']=trim($_POST['suborder']);
			if ( empty($_POST['mainorder']) || empty($_POST['suborder']) || $_POST['suborder']==$_POST['mainorder'])
			{
					
					trigger_error( "订单号没有输入或订单号一样", E_USER_ERROR );
			}
			
			$objOrder = $this->system->loadmodel( "trading/order" );
			$aField = array("member_id","status","pay_status","ship_status","shipping_id","weight","itemnum","cost_item","cost_freight","score_g","discount","total_amount","final_amount","pmt_amount","payed","tostr");
			$mainorderinfo = $objOrder->getfieldbyid( $_POST['mainorder'],$aField);
			$suborderinfo = $objOrder->getfieldbyid( $_POST['suborder'],$aField);
			if(!$mainorderinfo || !$suborderinfo){
				
				trigger_error( "订单号错误或不存在", E_USER_ERROR );
			}
			if(empty($mainorderinfo['member_id'])){
				
				trigger_error( "订单信息没有", E_USER_ERROR );
			
			}
			if($mainorderinfo['member_id']==$suborderinfo['member_id'] && $mainorderinfo['status']==$suborderinfo['status'] && $mainorderinfo['pay_status']==$suborderinfo['pay_status'] && $mainorderinfo['ship_status']==$suborderinfo['ship_status'] ){
					$data['order_id']=$_POST['mainorder'];
					$data['weight'] = $mainorderinfo['weight'] + $suborderinfo['weight'];
					$data['itemnum'] = intval($mainorderinfo['itemnum'] + $suborderinfo['itemnum']);
					$data['cost_item'] = $mainorderinfo['cost_item'] + $suborderinfo['cost_item'];
					$data['cost_freight'] = $mainorderinfo['cost_freight'] + $suborderinfo['cost_freight'];
					$data['score_g'] = intval($mainorderinfo['score_g'] + $suborderinfo['score_g']);
					$data['discount'] = $mainorderinfo['discount'] + $suborderinfo['discount'];
					$data['total_amount'] = $mainorderinfo['total_amount'] + $suborderinfo['total_amount'];
					$data['final_amount'] = $mainorderinfo['final_amount'] + $suborderinfo['final_amount'];
					$data['pmt_amount'] = $mainorderinfo['pmt_amount'] + $suborderinfo['pmt_amount'];
					$data['payed'] = $mainorderinfo['payed'] + $suborderinfo['payed'];
					$data['tostr'] = $mainorderinfo['tostr'].",".$suborderinfo['tostr'];
					$objOrder->op_id = $this->op->opid;
					$objOrder->op_name = $this->op->loginName;
					$this->end( $objOrder->dotogether( $_POST['mainorder'],$_POST['suborder'],$data,$suborderinfo['ship_status']),__( "订单合并成功" ) );
				
			}
			else {
			
				trigger_error( "2个订单状态不一致，不能合并", E_USER_ERROR );
				
			}
			
	}

}

?>